package com.yunquan.ohana.ui.guide.adapter

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.yunquan.ohana.R

class GuideAdapter : RecyclerView.Adapter<GuideAdapter.ViewHolder>() {

    private var onSkipListener: (() -> Unit?)? = null
    private var onNextListener: (() -> Unit?)? = null

    fun setOnSkipClickListener(listener: () -> Unit) {
        onSkipListener = listener
    }

    fun setOnNextClickListener(listener: () -> Unit) {
        onNextListener = listener
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_guide, parent, false)
        return ViewHolder(view)
    }

    override fun getItemCount(): Int {
        return 3
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.bind(position)
    }

    inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        private val iv01: ImageView = itemView.findViewById(R.id.iv_01)
        private val iv102: ImageView = itemView.findViewById(R.id.iv_102)
        private val iv202: ImageView = itemView.findViewById(R.id.iv_202)
        private val iv302: ImageView = itemView.findViewById(R.id.iv_302)
        private val tv01: TextView = itemView.findViewById(R.id.tv_01)
        private val tv02: TextView = itemView.findViewById(R.id.tv_02)
        private val ivCheck01: ImageView = itemView.findViewById(R.id.iv_check_01)
        private val ivCheck02: ImageView = itemView.findViewById(R.id.iv_check_02)
        private val ivCheck03: ImageView = itemView.findViewById(R.id.iv_check_03)
        private val btnSkip: TextView = itemView.findViewById(R.id.btn_skip)
        private val btnNext: TextView = itemView.findViewById(R.id.btn_next)

        @SuppressLint("SetTextI18n")
        fun bind(position: Int) {
            btnNext.setOnClickListener { onNextListener?.invoke() }
            btnSkip.setOnClickListener { onSkipListener?.invoke() }

            when (position) {
                1 -> {
                    iv102.visibility = View.INVISIBLE
                    iv202.visibility = View.VISIBLE
                    iv302.visibility = View.INVISIBLE
                    iv01.setImageResource(R.mipmap.ic_guide_201)
                    tv01.setText(R.string.tip_guide_201)
                    tv02.setText(R.string.tip_guide_202)
                    ivCheck01.setImageResource(R.mipmap.ic_guide_false)
                    ivCheck02.setImageResource(R.mipmap.ic_guide_true)
                    ivCheck03.setImageResource(R.mipmap.ic_guide_false)
                }

                2 -> {
                    iv102.visibility = View.INVISIBLE
                    iv202.visibility = View.INVISIBLE
                    iv302.visibility = View.VISIBLE
                    iv01.setImageResource(R.mipmap.ic_guide_301)
                    tv01.setText(R.string.tip_guide_301)
                    tv02.setText(R.string.tip_guide_302)
                    ivCheck01.setImageResource(R.mipmap.ic_guide_false)
                    ivCheck02.setImageResource(R.mipmap.ic_guide_false)
                    ivCheck03.setImageResource(R.mipmap.ic_guide_true)
                }

                else -> {
                    iv102.visibility = View.VISIBLE
                    iv202.visibility = View.INVISIBLE
                    iv302.visibility = View.INVISIBLE
                    iv01.setImageResource(R.mipmap.ic_guide_101)
                    tv01.setText(R.string.tip_guide_101)
                    tv02.setText(R.string.tip_guide_102)
                    ivCheck01.setImageResource(R.mipmap.ic_guide_true)
                    ivCheck02.setImageResource(R.mipmap.ic_guide_false)
                    ivCheck03.setImageResource(R.mipmap.ic_guide_false)
                }
            }
        }
    }
}